package com.winchain.chainbill.risk.biz.access.controller;

import com.shell.bee.base.annotation.Logging;
import com.shell.bee.entity.mvc.RetMsg;
import com.shell.bee.file.service.PubFileUploadService;
import com.shell.bee.file.vo.FileWapper;
import com.shell.bee.mvc.controller.BaseController;
import com.shell.bee.persistence.model.PageLimit;
import com.shell.framework.utils.CommonFileUtil;
import com.winchain.chainbill.bill.biz.send.controller.BillSendController;
import com.winchain.chainbill.risk.base.po.RmAccess;
import com.winchain.chainbill.risk.base.service.RmAccService;
import com.winchain.chainbill.risk.biz.access.service.RmAccessImptService;
import com.winchain.chainbill.risk.biz.access.service.RmAccessModService;
import com.winchain.chainbill.risk.biz.access.vo.AccessVo;
import java.io.InputStream;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class AccessController
  extends BaseController
{
  @Autowired
  private PubFileUploadService fileLocalUploadService;
  @Autowired
  private RmAccService rmAccService;
  @Autowired
  private RmAccessImptService rmAccessImptService;
  @Autowired
  private RmAccessModService rmAccessModService;
  
  @RequestMapping
  @ResponseBody
  @Logging(module="��������", methods="����������������")
  public List<AccessVo> findAccesss(PageLimit limit, RmAccess rmAccess)
    throws Exception
  {
    if (rmAccess == null) {
      rmAccess = new RmAccess();
    }
    if (rmAccess.getBuyerNm() != null) {
      rmAccess.setBuyerNm("%" + rmAccess.getBuyerNm() + "%");
    }
    if (rmAccess.getSellerNm() != null) {
      rmAccess.setSellerNm("%" + rmAccess.getSellerNm() + "%");
    }
    if ((null == rmAccess.getLoanAvl()) || ("-1".equals(rmAccess.getLoanAvl()))) {
      rmAccess.setLoanAvl(null);
    }
    if ((null == rmAccess.getOutputAvl()) || ("-1".equals(rmAccess.getOutputAvl()))) {
      rmAccess.setOutputAvl(null);
    }
    List<AccessVo> list = this.rmAccService.selectAccessForPage(limit, rmAccess);
    
    return list;
  }
  
  @RequestMapping
  @ResponseBody
  @Logging(module="��������", methods="������������")
  public RmAccess findAccess(String rateBizType, String accId)
    throws Exception
  {
    RmAccess rmAccess = new RmAccess();
    rmAccess.setAccId(accId);
    return (RmAccess)this.rmAccService.selectByKey(rmAccess);
  }
  
  @RequestMapping
  @ResponseBody
  @Logging(module="��������", methods="������������")
  public RetMsg addAccess(AccessVo accessVo)
    throws Exception
  {
    this.rmAccessModService.addAccess(accessVo);
    return initRetMsg("success", "��������");
  }
  
  @RequestMapping
  @ResponseBody
  @Logging(module="��������", methods="������������")
  public RetMsg updateAccess(RmAccess rmAccess)
    throws Exception
  {
    this.rmAccessModService.mod(rmAccess);
    return initRetMsg("success", "��������");
  }
  
  @RequestMapping
  @ResponseBody
  @Logging(module="��������", methods="������������")
  public RetMsg deleteAccess(String accId)
    throws Exception
  {
    int re = this.rmAccessModService.deleteAccess(accId);
    if (re == 1) {
      return initRetMsg("success", "��������");
    }
    return initRetMsg("error", "��������");
  }
  
  @RequestMapping
  @ResponseBody
  @Logging(module="��������", methods="������������")
  public RetMsg read(String fileId)
    throws Exception
  {
    this.rmAccessImptService.read(this.fileLocalUploadService.getDownloadFile(fileId).getFile());
    
    return initRetMsg("success", "��������");
  }
  
  @RequestMapping
  @Logging(module="��������", methods="��������")
  public void downloadTemplate(HttpServletRequest request, HttpServletResponse response)
    throws Exception
  {
    String pathValue = "/template/excel-import/FinRmAccessImport.xlsx";
    String fileName = "����������������.xlsx";
    InputStream is = BillSendController.class.getResourceAsStream(pathValue);
    CommonFileUtil.writeFileToClient(fileName, is, response, request);
  }
}
